// ImageLib.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "ImageLib.h"

IWICImagingFactory   *g_theImagingFactory       = NULL;
IWICComponentFactory *g_theIWICComponentFactory = NULL;

HRESULT g_hrLast = S_OK;

CLASS_DECLSPEC HRESULT GetImageLibLastError()
{
    return g_hrLast;
}

CLASS_DECLSPEC HRESULT InitImagingFactory()
{
    // Initialize WindowsCodecs
    HRESULT hr = S_OK;
    if(g_theImagingFactory == NULL)
    {
        hr = ::CoCreateInstance(
            CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*) &g_theImagingFactory);
        // Success or not
        if (SUCCEEDED(hr))
        {
            hr = g_theImagingFactory->QueryInterface(
                IID_IWICComponentFactory, (void**)&g_theIWICComponentFactory);
            if(FAILED(hr))
            {
                g_theIWICComponentFactory = NULL;
                ::OutputDebugStringW(L"ERROR: Create IWICComponentFactory fail.");
            }
        }
        else
        {
            g_theImagingFactory = NULL;
            g_theIWICComponentFactory = NULL;
            ::OutputDebugStringW(L"ERROR: Create IWICImagingFactory fail.");
        }
    }
    g_hrLast = hr;
    return hr;
}

CLASS_DECLSPEC void ReleaseImagingFactory()
{
    if(g_theImagingFactory != NULL)
    {
        g_theImagingFactory->Release();
    }
    g_theImagingFactory = NULL;

    if(g_theIWICComponentFactory != NULL)
    {
        g_theIWICComponentFactory->Release();
    }
    g_theIWICComponentFactory = NULL;
}



